/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#include <sxVega_EulerIntegrator.h>

/* VegaFEM Includes */
#include <eulerSparse.h>
#include <implicitBackwardEulerSparse.h>

const static double DEFAULT_TIMESTEP = (1.0 / 30.0);

Sx::Vega::Physics::Vega_EulerIntegrator::Vega_EulerIntegrator(unsigned int nodeCount, SparseMatrix* massMatrix, ForceModel* forceModel) : Vega_Integrator(nodeCount) {
	this->nodeCount = nodeCount;
	this->integrator = new ImplicitBackwardEulerSparse(this->nodeCount * 3, DEFAULT_TIMESTEP, massMatrix, forceModel, 1);
	this->integrator->SetDampingMassCoef(1.01f);
}

Sx::Vega::Physics::Vega_EulerIntegrator::~Vega_EulerIntegrator() {
	delete this->integrator;
}

bool Sx::Vega::Physics::Vega_EulerIntegrator::addNodeExternalForce(unsigned int index, const Eigen::Vector3d& force) {
	if ( this->integrator == nullptr ) return false;
	if ( index >= this->nodeCount ) return false;
	unsigned int index3 = index * 3;

	this->integrator->GetExternalForces()[index3] += force.x();
	this->integrator->GetExternalForces()[index3 + 1] += force.y();
	this->integrator->GetExternalForces()[index3 + 2] += force.z();
	return true;
}

bool Sx::Vega::Physics::Vega_EulerIntegrator::addNodeVelocity(unsigned int index, const Eigen::Vector3d& velocity) {
	if ( this->integrator == nullptr ) return false;
	if ( index >= this->nodeCount ) return false;
	unsigned int index3 = index * 3;

	this->integrator->GetExternalForces()[index3] += velocity.x();
	this->integrator->GetExternalForces()[index3 + 1] += velocity.y();
	this->integrator->GetExternalForces()[index3 + 2] += velocity.z();
	return true;
}

bool Sx::Vega::Physics::Vega_EulerIntegrator::addNodePosition(unsigned int index, const Eigen::Vector3d& position) {
	if ( this->integrator == nullptr ) return false;
	if ( index >= this->nodeCount ) return false;
	unsigned int index3 = index * 3;

	this->integrator->Getq()[index3] += position.x();
	this->integrator->Getq()[index3 + 1] += position.y();
	this->integrator->Getq()[index3 + 2] += position.z();
	return true;
}

bool Sx::Vega::Physics::Vega_EulerIntegrator::setNodeExternalForce(unsigned int index, const Eigen::Vector3d& force) {
	if ( this->integrator == nullptr ) return false;
	if ( index >= this->nodeCount ) return false;
	unsigned int index3 = index * 3;

	this->integrator->GetExternalForces()[index3] = force.x();
	this->integrator->GetExternalForces()[index3 + 1] = force.y();
	this->integrator->GetExternalForces()[index3 + 2] = force.z();
	return true;
}

bool Sx::Vega::Physics::Vega_EulerIntegrator::setNodeExternalForce(unsigned int index, double x, double y, double z) {
	if ( this->integrator == nullptr ) return false;
	if ( index >= this->nodeCount ) return false;
	unsigned int index3 = index * 3;

	this->integrator->GetExternalForces()[index3] = x;
	this->integrator->GetExternalForces()[index3 + 1] = y;
	this->integrator->GetExternalForces()[index3 + 2] = z;
	return true;
}

bool Sx::Vega::Physics::Vega_EulerIntegrator::setNodeInternalForce(unsigned int index, const Eigen::Vector3d& force) {
	if ( this->integrator == nullptr ) return false;
	if ( index >= this->nodeCount ) return false;
	unsigned int index3 = index * 3;

	this->integrator->GetInternalForces()[index3] = force.x();
	this->integrator->GetInternalForces()[index3 + 1] = force.y();
	this->integrator->GetInternalForces()[index3 + 2] = force.z();
	return true;
}

bool Sx::Vega::Physics::Vega_EulerIntegrator::setNodeInternalForce(unsigned int index, double x, double y, double z) {
	if ( this->integrator == nullptr ) return false;
	if ( index >= this->nodeCount ) return false;
	unsigned int index3 = index * 3;

	this->integrator->GetInternalForces()[index3] = x;
	this->integrator->GetInternalForces()[index3 + 1] = y;
	this->integrator->GetInternalForces()[index3 + 2] = z;
	return true;
}

bool Sx::Vega::Physics::Vega_EulerIntegrator::setNodeVelocity(unsigned int index, const Eigen::Vector3d& velocity) {
	if ( this->integrator == nullptr ) return false;
	if ( index >= this->nodeCount ) return false;
	unsigned int index3 = index * 3;

	this->integrator->Getqvel()[index3] = velocity.x();
	this->integrator->Getqvel()[index3 + 1] = velocity.y();
	this->integrator->Getqvel()[index3 + 2] = velocity.z();
	return true;
}

bool Sx::Vega::Physics::Vega_EulerIntegrator::setNodeVelocity(unsigned int index, double x, double y, double z) {
	if ( this->integrator == nullptr ) return false;
	if ( index >= this->nodeCount ) return false;
	unsigned int index3 = index * 3;

	this->integrator->Getqvel()[index3] = x;
	this->integrator->Getqvel()[index3 + 1] = y;
	this->integrator->Getqvel()[index3 + 2] = z;
	return true;
}

bool Sx::Vega::Physics::Vega_EulerIntegrator::setNodePosition(unsigned int index, const Eigen::Vector3d& position) {
	if ( this->integrator == nullptr ) return false;
	if ( index >= this->nodeCount ) return false;
	unsigned int index3 = index * 3;

	this->integrator->Getq()[index3] = position.x() - this->restPositions[index3];
	this->integrator->Getq()[index3 + 1] = position.y() - this->restPositions[index3 + 1];
	this->integrator->Getq()[index3 + 2] = position.z() - this->restPositions[index3 + 2];
	return true;
}

bool Sx::Vega::Physics::Vega_EulerIntegrator::setNodePosition(unsigned int index, double x, double y, double z) {
	if ( this->integrator == nullptr ) return false;
	if ( index >= this->nodeCount ) return false;
	unsigned int index3 = index * 3;

	this->integrator->Getq()[index3] = x - this->restPositions[index3];
	this->integrator->Getq()[index3 + 1] = y - this->restPositions[index3 + 1];
	this->integrator->Getq()[index3 + 2] = z - this->restPositions[index3 + 2];
	return true;
}

bool Sx::Vega::Physics::Vega_EulerIntegrator::setNodeAcceleration(unsigned int index, const Eigen::Vector3d& acceleration) {
	if ( this->integrator == nullptr ) return false;
	if ( index >= this->nodeCount ) return false;
	unsigned int index3 = index * 3;

	this->integrator->Getqaccel()[index3] = acceleration.x();
	this->integrator->Getqaccel()[index3 + 1] = acceleration.y();
	this->integrator->Getqaccel()[index3 + 2] = acceleration.z();
	return true;
}

bool Sx::Vega::Physics::Vega_EulerIntegrator::setNodeAcceleration(unsigned int index, double x, double y, double z) {
	if ( this->integrator == nullptr ) return false;
	if ( index >= this->nodeCount ) return false;
	unsigned int index3 = index * 3;

	this->integrator->Getqaccel()[index3] = x;
	this->integrator->Getqaccel()[index3 + 1] = y;
	this->integrator->Getqaccel()[index3 + 2] = z;
	return true;
}

Eigen::Vector3d Sx::Vega::Physics::Vega_EulerIntegrator::getNodeExternalForce(unsigned int index) {
	if ( this->integrator == nullptr ) return Eigen::Vector3d::Zero();
	if ( index >= this->nodeCount ) return Eigen::Vector3d::Zero();
	unsigned int index3 = index * 3;

	Eigen::Vector3d externalForce;
	externalForce.x() = this->integrator->GetExternalForces()[index3];
	externalForce.y() = this->integrator->GetExternalForces()[index3 + 1];
	externalForce.z() = this->integrator->GetExternalForces()[index3 + 2];
	return externalForce;
}

Eigen::Vector3d Sx::Vega::Physics::Vega_EulerIntegrator::getNodeInternalForce(unsigned int index) {
	if ( this->integrator == nullptr ) return Eigen::Vector3d::Zero();
	if ( index >= this->nodeCount ) return Eigen::Vector3d::Zero();
	unsigned int index3 = index * 3;

	Eigen::Vector3d internalForce;
	internalForce.x() = this->integrator->GetInternalForces()[index3];
	internalForce.y() = this->integrator->GetInternalForces()[index3 + 1];
	internalForce.z() = this->integrator->GetInternalForces()[index3 + 2];
	return internalForce;
}

Eigen::Vector3d Sx::Vega::Physics::Vega_EulerIntegrator::getNodeAcceleration(unsigned int index) {
	if ( this->integrator == nullptr ) return Eigen::Vector3d::Zero();
	if ( index >= this->nodeCount ) return Eigen::Vector3d::Zero();
	unsigned int index3 = index * 3;

	Eigen::Vector3d acceleration;
	acceleration.x() = this->integrator->Getqaccel()[index3];
	acceleration.y() = this->integrator->Getqaccel()[index3 + 1];
	acceleration.z() = this->integrator->Getqaccel()[index3 + 2];
	return acceleration;
}

Eigen::Vector3d Sx::Vega::Physics::Vega_EulerIntegrator::getNodeVelocity(unsigned int index) {
	if ( this->integrator == nullptr ) return Eigen::Vector3d::Zero();
	if ( index >= this->nodeCount ) return Eigen::Vector3d::Zero();
	unsigned int index3 = index * 3;

	Eigen::Vector3d velocity;
	velocity.x() = this->integrator->Getqvel()[index3];
	velocity.y() = this->integrator->Getqvel()[index3 + 1];
	velocity.z() = this->integrator->Getqvel()[index3 + 2];
	return velocity;
}

Eigen::Vector3d Sx::Vega::Physics::Vega_EulerIntegrator::getNodePosition(unsigned int index) {
	Eigen::Vector3d position;
	position.x() = 0;
	position.y() = 0;
	position.z() = 0;

	if ( this->integrator == nullptr ) return position;
	if ( this->restPositions == nullptr ) return position;
	if ( index >= this->nodeCount ) return position;

	unsigned int index3 = index * 3;

	position.x() = this->restPositions[index3] + this->integrator->Getq()[index3];
	position.y() = this->restPositions[index3 + 1] + this->integrator->Getq()[index3 + 1];
	position.z() = this->restPositions[index3 + 2] + this->integrator->Getq()[index3 + 2];
	return position;
}

void Sx::Vega::Physics::Vega_EulerIntegrator::performTimestep(double dt) {
	if ( this->integrator == nullptr ) return;
	this->integrator->SetTimestep(dt);
	this->integrator->DoTimestep();
}

void Sx::Vega::Physics::Vega_EulerIntegrator::reset() {
	if ( this->integrator == nullptr ) return;
	this->integrator->SetExternalForcesToZero();
	this->integrator->ResetToRest();
}

void Sx::Vega::Physics::Vega_EulerIntegrator::zeroExternalForces() {
	if ( this->integrator == nullptr ) return;
	this->integrator->SetExternalForcesToZero();
}

bool Sx::Vega::Physics::Vega_EulerIntegrator::isSymplectic() const {
	return true;
}
